package cn.tedu.sp04.service;

import cn.tedu.sp04.feign.ItemClient;
import cn.tedu.sp04.feign.UserClient;
import com.netflix.discovery.converters.Auto;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import pojo.Item;
import pojo.Order;
import pojo.User;
import service.OrderService;

import java.util.List;

@Slf4j
@Service
public class OrderServiceImpl implements OrderService {

    @Autowired
    private UserClient userClient;

    @Autowired
    private ItemClient itemClient;

    @Override
    public Order getOrder(String orderId) {
        User user = userClient.getUser(Integer.parseInt(orderId)).getData();
        List<Item> items = itemClient.getItems(orderId).getData();
        Order order = new Order();
        order.setId(orderId);
        order.setUser(user);
        order.setItems(items);
        return order;
    }

    @Override
    public void addOrder(Order order) {
        List<Item> items = order.getItems();
        itemClient.decreaseNumber(items);
        User user = order.getUser();
        userClient.addScore(user.getId(), 10);
        log.info("保存订单:{}", order);
    }
}
